package main

import (
	"fmt"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"log"
)

func main() {
	pflag.Parse()
	viper.SetConfigName("config")
	viper.SetConfigType("toml")
	viper.AddConfigPath(".")
	viper.SetDefault("redis.port", 6381)
	err := viper.ReadInConfig()
	if err != nil {
		log.Fatal("read config failed: %v", err)
	}
	//viper.OnConfigChange(func(in fsnotify.Event) {
	//	fmt.Println(in.Name)
	//	fmt.Println(in.Op.String())
	//})
	fmt.Println(viper.Get("app_name"))
	fmt.Println(viper.Get("redis.port"))
	fmt.Println(viper.Get("log_level"))
	fmt.Println("mysql ip: ", viper.Get("mysql.ip"))
	fmt.Println("mysql port: ", viper.Get("mysql.port"))
	fmt.Println("mysql user: ", viper.Get("mysql.user"))
	fmt.Println("mysql password ", viper.Get("mysql.password"))
	fmt.Println("mysql database ", viper.Get("mysql.database"))

	fmt.Println("redis ip: ", viper.Get("redis.ip"))
	fmt.Println("redis port: ", viper.Get("redis.port"))
	viper.SetDefault("redis.sin", 123)
	fmt.Println("redis sin :", viper.Get("redis.sin"))

	fmt.Println("protocols:", viper.GetStringSlice("server.protocols"))
	fmt.Println("ports: ", viper.GetIntSlice("server.ports"))
	fmt.Println("timeout:", viper.GetDuration("server.timeout"))
	fmt.Println("accmode:", viper.GetString("server.accmode"))
	fmt.Println("accint:", viper.GetInt("server.accmode"))

	if viper.IsSet("redis.port") {
		fmt.Println("redis.port is set")
	} else {
		fmt.Println("redis.port is not set")
	}

	fmt.Println("mysql settings: ", viper.GetStringMap("mysql"))
	fmt.Println("redis settings: ", viper.GetStringMap("redis"))
	fmt.Println("all settings: ", viper.AllKeys())
	fmt.Println("all settings value", viper.AllSettings())
	//redisInit()
	fmt.Println("redis.port:", viper.Get("redis.port"))
	fmt.Println("redis.ip:", viper.Get("redis.ip"))

	fmt.Println("GOPATH", viper.Get("GOPATH"))
	fmt.Println("MSF_ENV", viper.Get("MSF_ENV"))

	fmt.Println("runmode:", viper.Get("runmode"))
	fmt.Println("go.path:", viper.Get("go.path"))
}
func init() {
	//pflag.Int("redis.port",8401,"redis port to connect")
	//pflag.String("redis.ip","127.0.0.1","redis.ip")
	//viper.BindPFlags(pflag.CommandLine)
	viper.AutomaticEnv()
	//绑定环境变量
	viper.BindEnv("runmode", "MSF_ENV")
	viper.BindEnv("go.path", "GOPATH")
}
func redisInit() {
	//viper.Set("redis.port",3301)
}
